package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"
	"time"

	"gitee.com/zhangxinzx/rolling-number/pkg/rolling"
)

func main() {
	secRollingWin, err := rolling.NewSecondRollingWindow(10 * time.Second)
	if err != nil {
		panic(err)
	}
	sigChan := make(chan os.Signal, 1)
	printFunc := func() {
		ticker := time.NewTicker(500 * time.Millisecond)
		for {
			select {
			case <-ticker.C:
				fmt.Println(secRollingWin)
			case <-sigChan:
				secRollingWin.Stop()
				fmt.Println(secRollingWin, " second rolling window summary: ", secRollingWin.Sum())
				return
			}
		}
	}
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go printFunc()
	for i := 0; i < 100; i++ {
		time.Sleep(50 * time.Millisecond)
		secRollingWin.Add(1)
	}
	process, err := os.FindProcess(os.Getpid())
	if err != nil {
		panic(err)
	}
	process.Signal(syscall.SIGINT)
	time.Sleep(time.Second)
}
